<template>
  <div class="header-wrap">
    <div class="header-left">
      <Hamburger @toggle="fnCollapse" />
      <div
        class="header-left-item"
        v-for="(item, index) in levelList"
        :key="index"
      >
        <span class="header-left-item-span">
          <router-link :to="item.path">{{ item.meta.title }}</router-link>
        </span>
        <span class="header-left-item-line">/</span>
      </div>
    </div>
    <div class="header-right">
      <div class="alarm">
        <Huo style="margin-right: 20px;"/>
        <Alarm ref="child_alarm" />
      </div>
      <Screenfull />
      <div class="user">
        <el-dropdown>
          <span class="el-dropdown-link">
            <i class="el-icon-s-custom" />
            {{ username }}
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="loginOut"
              ><i class="el-icon-s-fold"></i>退出</el-dropdown-item
            >
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script>
import Alarm from "./Alarm";
import Screenfull from "./Screenfull";
import Hamburger from "./Hamburger";
import { getData, postData } from "@/request/http";
import bus from "@/utils/bus";
import Huo from './Huo'

export default {
  name: "Header",
  data() {
    return {
      levelList: [],
      alarmNum: 0,
    };
  },
  computed: {
    username() {
      return this.$store.state.login.username;
    },
  },
  mounted() {},
  methods: {
    //退出登录
    loginOut() {
      postData("/adminweb/sys/admin/logout")
        .then((res) => {
          localStorage.clear();
          sessionStorage.clear();
          if (res.code == 0) {
            localStorage.removeItem("username");
            localStorage.removeItem("token");
            this.$store.commit("login/setUser", "");
          
          //  let host_login_url = "http://" + window.location.host + "/#/login";
            let host_login_url =   window.location.protocol+'//' + window.location.host + "/#/login";
            let global_login_url = window.loginUrl;
            window.location.href =
              global_login_url == "" ? host_login_url : global_login_url;
            if (window.mysocket) {
              window.reStartStatus = false;
              if (window.timerWs) {
                clearInterval(window.timerWs);
              }
              window.mysocket.close();
            }
          } else if (res.code == -56055) {
            return;
          } else {
            this.$message.error(res.code + "，" + res.msg);
          }
        })
        .catch((res) => {
          this.$message.error(res.code + "，" + res.msg);
        });
      //传给alarm，去断开WS
      //   if (window.mysocket) {
      //     window.mysocket.close();
      //     window.mysocket = null;
      //   }
    },

    /*
     * @desc 面包屑导航
     * */
    getBreadcrumb() {
      //$route.matched一个数组 包含当前路由的所有嵌套路径片段的路由记录
      let matched = this.$route.matched;
      // console.log(matched)
      this.levelList = matched;
    },

    /*
     * @desc 控制菜单展开缩放
     * */
    fnCollapse(val) {
      this.$emit("toggle", val);
    },
  },
  watch: {
    $route: {
      handler() {
        this.getBreadcrumb();
      },
      immediate: true,
    },
  },
  components: {
    Alarm,
    Screenfull,
    Hamburger,
    Huo
  },
};
</script>

<style scoped>
.el-header {
  padding: 0 !important;
}
.header-wrap {
  height: 60px;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}
.header-left {
  flex: 1;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  padding-left: 10px;
}
.header-left .logo-txt {
  color: #000;
  height: 60px;
  line-height: 60px;
  font-size: 18px;
  margin-left: 20px;
}
.header-left-icon {
  color: #000;
  margin-right: 10px;
  cursor: pointer;
  font-size: 25px;
}
.header-left-item {
  color: #000;
  margin-right: 3px;
  font-size: 14px;
  font-weight: 400 !important;
}
.header-left-item-line {
  margin: 0 9px;
  font-weight: 700;
  color: #969896;
}
.header-right {
  width: 22%;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-right: 40px;
  color: #000;
  font-size: 15px;
  font-weight: 600;
}
.alarm {
  width: 70px;
  height: 20px;
  position: relative;
  margin-right: 5px;
}
/* .alarmNum {
  background-color: #da2727;
  position: absolute;
  right: 11px;
  top: -8px;
  width: 17px;
  height: 17px;
  line-height: 17px;
  color: #fff;
  font-size: 12px;
  text-align: center;
  border-radius: 50%;
} */
.header-right .user {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-right: 20px;
}
.header-right .user img {
  width: 20px;
  height: 20px;
  display: block;
  margin-right: 5px;
}
.el-icon-s-custom {
  font-size: 21px;
  cursor: pointer;
}
.header-left-item-span a {
  color: #676a6d;
  text-decoration: none;
}

/* 暗黑模式 */
.black {
  height: 60px;
  background-color: rgb(48, 65, 86);
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-shadow: 0 1px 4px rgb(0 21 41 / 8%);
  box-shadow: 0 1px 4px rgb(0 21 41 / 8%);
}
.black .header-left-item-span a {
  color: #fff;
}
.black .el-icon-s-custom {
  color: #fff;
}
.black .el-dropdown {
  color: #fff;
}
</style>
